using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public enum WeaponType
{
    Gun = 0, Rifle = 1, Rocket = 2,
    MAX
}
// 抽象类不能实例化,它们通常作为其他类的基类,以提供共同的功能
// 抽象类可以包含抽象方法和非抽象方法,属性和字段,以及事件和索引器
// 抽象方法是未实现的方法,必须在派生类中实现即子类必须重写,定义为abstract修饰符且没有方法体
// 虚方法是可以被派生类重写的非抽象方法,定义为virtual修饰符且有一个方法体
// 虚方法可以被派生类重写,也可以不被重写,而 abstract 抽象方法必须被重写,否则报错 `does not implement inherited abstract member`

public abstract class IWeapon
{
    protected WeaponBaseAttr mBaseAttr; // 武器基础属性
    protected GameObject mGameObject; //武器游戏对象
    public GameObject gameObject { get { return mGameObject; } }
    protected Light mLight;

    // 攻击效果
    protected ParticleSystem mPariticle; //子弹粒子特效
    protected LineRenderer mLine; //子弹轨迹
    protected AudioSource mAudio; //枪声

    protected float mEffectDisplayTime = 0; //特效显示时间

    public ICharacter mOwner;
    public ICharacter owner { set { mOwner = value; } get { return mOwner; } } //持有者

    // 构造函数
    public IWeapon(WeaponBaseAttr baseAttr, GameObject gameObject)
    {
        mBaseAttr = baseAttr;
        mGameObject = gameObject;

        Transform effect = mGameObject.transform.Find("Effect");
        mPariticle = effect.GetComponent<ParticleSystem>();
        mLine = effect.GetComponent<LineRenderer>();
        mLight = effect.GetComponent<Light>();
        mAudio = effect.GetComponent<AudioSource>();
    }


    // 武器的攻击范围
    public float atkRange { get { return mBaseAttr.atkRange; } }

    public int atk { get { return mBaseAttr.atk; } }

    // 模板方法的整体框架流程

    public virtual void Fire(Vector3 targetPosition)
    {
        //显示枪口特效
        PlayMuzzleEffect();

        //显示子弹轨迹特效
        PlayBulletEffect(targetPosition);

        //设置特效显示时间
        SetEffetDisplayTime();

        //播放声音
        PlaySound();
        // Debug.Log("Fire");
    }

    protected virtual void PlayMuzzleEffect()
    {
        mPariticle.Stop();
        mPariticle.Play();
        mLight.enabled = true;
    }

    protected abstract void PlayBulletEffect(Vector3 targetPosition);
    protected abstract void SetEffetDisplayTime();

    protected abstract void PlaySound();

    // 模板方法的参数变化,让子类灵活修改
    protected void DoPlaySound(string clipName)
    {
        // AudioClip clip = FactoryManager.assetFactory.LoadAudioClip(clipName);
        // mAudio.clip = clip;
        // mAudio.Play();
    }

    protected void DoPlayBulletEffect(float width, Vector3 targetPosition)
    {
        mLine.enabled = true;
        mLine.startWidth = width;
        mLine.endWidth = width;
        mLine.SetPosition(0, mGameObject.transform.position);
        mLine.SetPosition(1, targetPosition);
    }

    public void Update()
    {
        if (mEffectDisplayTime > 0)
        {
            mEffectDisplayTime -= Time.deltaTime;
            if (mEffectDisplayTime <= 0)
            {
                DisableEffect();
            }
        }
    }

    private void DisableEffect()
    {
        mLine.enabled = false;
        mLight.enabled = false;
    }
}